import Cookies from 'js-cookie';
import { v4 as uuidv4 } from 'uuid';
import JSEncrypt from 'jsencrypt'
const Layout = () => import('@/layouts/index.vue')
const publicKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiFCCvRYxdFqfGMnwI6G7tIW5zwwOxLbqs2F7LfeTERnKDzjF2f7rbcBPLTYOqxNUYvvDBe8Bg1k/tNOqih2TEinF/x3bKRg/mWmFNQXm2taMs0lV03TC0cIkuCg0uZpxxUta4vkdXVaGp47MT/TqZ72560AYrnv6Kb6TRTWTredq09WEcrUPrxITUkgrY7rVysL5pl13Xga4zJqH8mgerlUU4Tg0lw+DImOwJjF2iPbseGaUPy881kFylqOdyr8w9elvQ6oN4QkUMvIWpWdV64vFi61D+IRSfJlLzy1PEdpshb5mmeepPbflYz/ttiaIQrrh0WJ4KP0Ih/aAvCDzJwIDAQAB
-----END PUBLIC KEY-----`
const viteComponents = import.meta.glob("../views/**/*.vue");
// const layoutviteComponents= import.meta.glob("../**/*.vue");
export default {
    createUuid() {
        return uuidv4();
    },
    encryption(value) {
        const encrypt = new JSEncrypt()
        encrypt.setPublicKey(publicKey)
        let cipher = encrypt.encrypt(value)
        return cipher
    },
    addRouter(routerlist) {
        const router = [];
        try {
            routerlist.forEach(e => {
                let e_new = {
                    id: e.id,
                    // component: !e.component?(viteComponents[`../layouts/index.vue`]):(viteComponents[`../${e.component}.vue`]),
                    // component:!e.component?
                    //     (resolve) => require([`@/layouts/index.vue`], resolve):(resolve) => require([`@${e.component}.vue`], resolve),
                };
                if(e.component){
                    e_new = {
                        ...e_new,
                        component: viteComponents[`../views${e.component}.vue`],
                    };
                }else{
                    console.log("Layout");
                    e_new = {
                        ...e_new,
                        component: Layout,
                    };
                }
                if (e.path) {
                    e_new = {
                        ...e_new,
                        path: `${e.path}`,

                    };
                }
                if (e.name !== "") {
                    e_new = {
                        ...e_new,
                        meta: {
                            title: e.name,
                            requireAuth: true,
                            status: e.status,
                            hidden: e.hidden,
                            icon: e.icon,
                            type: e.type!==4&&e.type,//类型（1目录 2页面 3菜单 4按钮）
                            permission: e.permission
                        }
                    };
                }
                if (e.children) {
                    const children = this.addRouter(e.children);
                    // 保存权限
                    e_new = {
                        ...e_new,
                        children: children,
                    };
                    if(children.length>0){
                        e_new.redirect=`${children[0].path}`
                    }
                }
                router.push(e_new);
            });
        } catch (error) {
            console.error(error);
            return [];
        }
        return router;
    },
    // loadComponent(url) {
    //     let path =import.meta.glob(url);
    //     return path;
    // },
}